<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ip地址" prop="ipadress">
        <el-input
          v-model="queryParams.ipadress"
          placeholder="请输入ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录名" prop="user">
        <el-input
          v-model="queryParams.user"
          placeholder="请输入登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录密码" prop="psw">
        <el-input
          v-model="queryParams.psw"
          placeholder="请输入登录密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口号" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入端口号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input
          v-model="queryParams.notes"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.door_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="位置" prop="controlPosition">
        <el-input
          v-model="queryParams.controlPosition"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出入门类型" prop="controlType">
        <el-select v-model="queryParams.controlType" placeholder="请选择出入门类型" clearable>
          <el-option
            v-for="dict in dict.type.th_opendoor_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['doorcontrol:control:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['doorcontrol:control:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="controlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门禁" align="center" prop="id" />
      <el-table-column label="ip地址" align="center" prop="ipadress" />
      <el-table-column label="登录名" align="center" prop="user" />
      <el-table-column label="登录密码" align="center" prop="psw" />
      <el-table-column label="端口号" align="center" prop="port" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.door_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center" prop="controlPosition" />
      <el-table-column label="出入门类型" align="center" prop="controlType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.th_opendoor_type" :value="scope.row.controlType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['haikang:access:edit']">修改</el-button>
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)" v-hasPermi="['haikang:access:remove']">删除</el-button>
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-delete"
                     @click="hkLogin(scope.row)">登录</el-button>
          <el-button v-if="scope.row.status >= 1" size="mini" type="text" icon="el-icon-plus"
                     @click="addUser(scope.row)">添加用户</el-button>

          <el-button v-if="scope.row.status == 1" size="mini" type="text" icon="el-icon-delete"
                     @click="hkbf(scope.row)">布防</el-button>

          <el-button v-if="scope.row.status == 2" size="mini" type="text" icon="el-icon-delete"
                     @click="hkcf(scope.row)">撤防</el-button>

          <el-button v-if="scope.row.status >= 1" size="mini" type="text" icon="el-icon-delete"
                     @click="hkLogout(scope.row)">退出</el-button>
          <el-button  size="mini" type="text" icon="el-icon-s-home"
                     @click="jrxt(scope.row)">进入系统</el-button>
          <el-button  v-if="scope.row.controlPosition != 'menfang'" size="mini" type="text" icon="el-icon-s-help"
                      @click="tbsj(scope.row)">同步数据</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门禁管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ip地址" prop="ipadress">
          <el-input v-model="form.ipadress" placeholder="请输入ip地址" />
        </el-form-item>
        <el-form-item label="登录名" prop="user">
          <el-input v-model="form.user" placeholder="请输入登录名" />
        </el-form-item>
        <el-form-item label="登录密码" prop="psw">
          <el-input v-model="form.psw" placeholder="请输入登录密码" />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口号" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.door_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="controlPosition">
          <el-input v-model="form.controlPosition" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="出入门类型" prop="controlType">
          <el-select v-model="form.controlType" placeholder="请选择出入门类型">
            <el-option
              v-for="dict in dict.type.th_opendoor_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加门禁用户 -->
    <el-dialog :title="title" :visible.sync="adduser" width="500px" append-to-body>
      <el-form ref="form" :model="userform" :rules="rules" label-width="80px">
<!--        <el-form-item label="员工编号" prop="employeeNo">
          <el-input v-model="userform.employeeNo" placeholder="请输入员工编号" />
        </el-form-item>-->
        <el-form-item label="员工姓名" prop="uname">
          <el-input v-model="userform.uname" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="userform.userType" placeholder="请选择用户类型">
            <el-option
              v-for="dict in dict.type.th_doorcontrol_usertype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="使能有效期" prop="enable">
          <el-radio-group v-model="form.enable">
            <el-radio
              v-for="dict in dict.type.th_doorcontrol_enable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker
            v-model="userform.beginTime"
            type="datetime"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="userform.endTime"
            type="datetime"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上传照片" prop="photo">
          <image-upload1 v-model="userform.photo"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormuser">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listControl, getControl, delControl, addControl, updateControl,
  hklogin,logout,hkbf,hkcf,controladdUser
} from "@/api/doorcontrol/control";

export default {
  name: "Control",
  dicts: ['door_status', 'th_opendoor_type','th_doorcontrol_usertype'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门禁管理表格数据
      controlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      adduser: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipadress: null,
        user: null,
        psw: null,
        port: null,
        notes: null,
        status: null,
        controlPosition: null,
        controlType: null,
      },
      // 表单参数
      form: {},
      //用户表单
      userform:{},
      // 表单校验
      rules: {
        ipadress: [
          { required: true, message: "ip地址不能为空", trigger: "blur" }
        ],
        user: [
          { required: true, message: "登录名不能为空", trigger: "blur" }
        ],
        psw: [
          { required: true, message: "登录密码不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "门禁状态不能为空", trigger: "blur" }
        ],
        photo: [
          { required: true, message: "照片不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门禁管理列表 */
    getList() {
      this.loading = true;
      listControl(this.queryParams).then(response => {
        this.controlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.adduser = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ipadress: null,
        user: null,
        psw: null,
        port: null,
        notes: null,
        status: null,
        controlPosition: null,
        controlType: null,
        createTime: null,
        createBy: null
      };
      this.userform = {
        controlIds: null,
        uname: null,
        userType: null,
        beginTime: null,
        endTime: null,
        photo: null,
      }
      this.resetForm("form");
      this.resetForm("userform")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getControl(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateControl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addControl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    submitFormuser(){
      this.$refs["form"].validate(valid => {
        if (valid) {
            controladdUser(this.userform).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
      });
    },

    jrxt(row){
      window.open('http://'+row.ipadress,'_blank');
    },
    //同步数据 --- 和获取门禁机的数据保存在数据库，将之前数据库中的数据的状态status修改成1 批量添加最新的数据
    tbsj(row){
      
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除门禁管理编号为"' + ids + '"的数据项？').then(function() {
        return delControl(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('doorcontrol/control/export', {
        ...this.queryParams
      }, `control_${new Date().getTime()}.xlsx`)
    },

    hkLogin(row) {
      this.reset();
      const id = row.id || this.ids
      hklogin(id).then(response => {
        if (response.code == 200) {
          this.$modal.msgSuccess("登录成功");
          this.getList();
        } else {
          this.$modal.msgError("登录失败");
        }
      });
    },
    addUser(row){
      this.userform.controlIds = row.id;
      this.adduser =true;

    },


    hkbf(row) {
      this.reset();
      const id = row.id || this.ids
      hkbf(id).then(response => {
        if (response.code == 200) {
          this.$modal.msgSuccess("布防成功");
          this.getList();
        } else {
          this.$modal.msgError("布防失败");
        }
      });
    },

    hkcf(row) {
      this.reset();
      const id = row.id || this.ids
      hkcf(id).then(response => {
        if (response.code == 200) {
          this.$modal.msgSuccess("撤防成功");
          this.getList();
        } else {
          this.$modal.msgError("撤防失败");
        }
      });
    },

    /** */
    hkLogout(row) {
      this.reset();
      const id = row.id || this.ids
      logout(id).then(response => {
        if (response.code == 200) {
          this.$modal.msgSuccess("退出成功");
          this.getList();
        } else {
          this.$modal.msgError("退出失败");
          this.getList();
        }
      });
    },

  }
};
</script>
